using System;
using System.Collections;
using UnityEngine;

public class PanelLayerControl : MonoBehaviour
{
	private int originalPanelDepth;

	public UIPanel panel;

	public UIWidget screenInteractionBlock;

	public Particle2D particles;

	private void Awake()
	{
		originalPanelDepth = panel.depth;
	}

	public void AnimationStart(UIWidget widgetThatMustBeVisible)
	{
		panel.depth = widgetThatMustBeVisible.panel.depth + 1;
		UITools.SetActive(screenInteractionBlock, true);
		NavigationManager.instance.enableEscape = false;
	}

	public void AnimationEnd()
	{
		panel.depth = originalPanelDepth;
		UITools.SetActive(screenInteractionBlock, false);
		NavigationManager.instance.enableEscape = true;
	}

	public Coroutine ShowParticles(UIWidget widgetThatMustBeVisible, Transform from, Transform to, string spriteName, float duration, Action onComplete)
	{
		return StartCoroutine(DoShowParticles(widgetThatMustBeVisible, from, to, spriteName, duration, onComplete));
	}

	public IEnumerator DoShowParticles(UIWidget widgetThatMustBeVisible, Transform from, Transform to, string spriteName, float duration, Action onComplete)
	{
		AnimationStart(widgetThatMustBeVisible);
		particles.cachedTransform.position = from.position;
		particles.travelTo = to;
		particles.duration = duration;
		particles.SetSpriteName(spriteName);
		float time = Mathf.Min(duration, (float)particles.maxParticles * particles.spwnRate) + particles.maxLifeTime;
		particles.Play();
		yield return new WaitForSeconds(time);
		AnimationEnd();
		if (onComplete != null)
		{
			onComplete();
		}
	}

	public Coroutine ShowParticlesOnFill(UIWidget widgetThatMustBeVisible, UIWidget sourceSprite, UISprite fillSprite, UILabel countLabel, int fromIntValue, int toIntValue, string stringTemplate, float fromValue, float toValue, string spriteName, float duration, Action onComplete)
	{
		return StartCoroutine(DoShowParticlesOnFill(widgetThatMustBeVisible, sourceSprite, fillSprite, countLabel, fromIntValue, toIntValue, stringTemplate, fromValue, toValue, spriteName, duration, onComplete));
	}

	public IEnumerator DoShowParticlesOnFill(UIWidget widgetThatMustBeVisible, UIWidget sourceSprite, UISprite fillSprite, UILabel countLabel, int fromIntValue, int toIntValue, string stringTemplate, float fromValue, float toValue, string spriteName, float duration, Action onComplete)
	{
		AnimationStart(widgetThatMustBeVisible);
		bool isEnergyLost = toValue < fromValue;
		Debug.Log("Is Lost " + isEnergyLost);
		UIWidget from = ((!isEnergyLost) ? widgetThatMustBeVisible : sourceSprite);
		UIWidget to = ((!isEnergyLost) ? sourceSprite : widgetThatMustBeVisible);
		particles.cachedTransform.position = from.cachedTransform.position;
		particles.travelTo = to.cachedTransform;
		particles.duration = duration;
		particles.SetSpriteName(spriteName);
		float totalDuration = Mathf.Min(duration, (float)particles.maxParticles * particles.spwnRate) + particles.maxLifeTime;
		particles.Play();
		if (countLabel != null)
		{
			StartCoroutine(UIAnimTools.DoFillLabelFromTo(countLabel, fromIntValue, toIntValue, stringTemplate, totalDuration));
		}
		if (fillSprite != null)
		{
			StartCoroutine(UIAnimTools.DoFillFromTo(fillSprite, fromValue, toValue, totalDuration));
		}
		float delay = 0.2f;
		yield return new WaitForSeconds(totalDuration + delay);
		AnimationEnd();
		if (onComplete != null)
		{
			onComplete();
		}
	}
}
